package com.jingyanzi.channel.service.impl;

import java.util.Calendar;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jingyanzi.channel.dao.ChannelMapper;
import com.jingyanzi.channel.dao.ChanneltypeMapper;
import com.jingyanzi.channel.domain.ChannelDO;
import com.jingyanzi.channel.entity.Channel;
import com.jingyanzi.channel.entity.ChannelExample;
import com.jingyanzi.channel.entity.ChannelExample.Criteria;
import com.jingyanzi.channel.entity.Channeltype;
import com.jingyanzi.channel.query.ChannelQueryDO;
import com.jingyanzi.channel.result.ResultDO;
import com.jingyanzi.channel.result.ResultSupport;
import com.jingyanzi.channel.service.IChannelService;
import com.jingyanzi.channel.util.BeanUtilsExtends;
import com.jingyanzi.privilege.domain.EmployeeDO;
import com.jingyanzi.privilege.factory.PrivilegeFactory;
import com.jingyanzi.privilege.service.IEmployeeService;
import com.jingyanzi.sys.domain.DictionariesDO;
import com.jingyanzi.sys.domain.service.ISettingService;
import com.jingyanzi.sys.factory.SysFactory;

@Service("channelservice")
public class ChannelServiceImpl extends BaseServiceImpl implements IChannelService {

	@Autowired
	private ChannelMapper channelMapper;

	@Autowired
	private ChanneltypeMapper channeltypeMapper;

	private ISettingService getSettingService() {
		return SysFactory.getISettingService();
	}

	private IEmployeeService getEmployeeService() {
		return PrivilegeFactory.getEmployeeService();
	}

	@Override
	public ResultDO add(ChannelDO channelDO) {
		Channel channel = new Channel();
		com.jingyanzi.sys.domain.result.ResultDO resultDO = null;
		ResultSupport result = BeanUtilsExtends.copy(channel, channelDO);
		if (!result.isSuccess()) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "对象转换失败");
			result.setSuccess(false);
			return result;
		}
		channel.setCreatetime(Calendar.getInstance().getTime());
		channel.setIsDelete(1);
		int r = -1;

		try {
			if (channel.getChannelTypeId() != null) {
				Channeltype channeltype = channeltypeMapper.selectByPrimaryKey(channel.getChannelTypeId());
				if (channeltype == null) {
					result.setErrorCode(ResultDO.CHANNEL_ADD_ERROR);
					result.setErrorMsg("不存在该渠道分类");
					result.setSuccess(false);
					return result;
				}
				channel.setChannelTypeName(channeltype.getChannelName());
			}
			if (channel.getLevel() != null) {
				resultDO = getSettingService().getDictionariesById(channel.getLevel());
				DictionariesDO dictionariess = (DictionariesDO) resultDO.getModel(ResultSupport.FIRST_MODEL_KEY);
				if (dictionariess == null) {
					result.setErrorCode(ResultDO.CHANNEL_ADD_ERROR);
					result.setErrorMsg("不存在该字典信息");
					result.setSuccess(false);
					return result;
				}
				channel.setLevelName(dictionariess.getDataItem());
			}
			if (channel.getStatus() != null) {
				resultDO = getSettingService().getDictionariesById(channel.getStatus());
				DictionariesDO dictionaries = (DictionariesDO) resultDO.getModel(ResultSupport.FIRST_MODEL_KEY);
				if (dictionaries == null) {
					result.setErrorCode(ResultDO.CHANNEL_ADD_ERROR);
					result.setErrorMsg("不存在该字典信息");
					result.setSuccess(false);
					return result;
				}
				channel.setStatusName(dictionaries.getDataItem());
			}
			if (channel.getProvince() != null) {
				channel.setRegion(channel.getProvince());
			}

			if (channel.getCity() != null) {
				channel.setRegion(channel.getRegion() + "/" + channel.getCity());
			}

			if (channel.getDistrict() != null) {
				channel.setRegion(channel.getRegion() + "/" + channel.getDistrict());
			}
			r = channelMapper.insert(channel);
		} catch (Exception e) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "添加渠道信息失败");
			result.setSuccess(false);
			return result;
		}

		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "添加渠道信息失败");
			result.setSuccess(false);
			return result;
		}

		result.setModel(ResultDO.FIRST_MODEL_KEY, channel.getId());
		return result;
	}

	@Override
	public ResultDO modifi(ChannelDO channelDO) {
		Channel channel = new Channel();
		com.jingyanzi.sys.domain.result.ResultDO resultDO = null;
		ResultSupport result = BeanUtilsExtends.copy(channel, channelDO);
		if (!result.isSuccess()) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "对象转换失败");
			result.setSuccess(false);
			return result;
		}
		int r = -1;

		try {
			if (channel.getChannelTypeId() != null) {
				Channeltype channeltype = channeltypeMapper.selectByPrimaryKey(channel.getChannelTypeId());
				channel.setChannelTypeName(channeltype.getChannelName());
			}
			if (channel.getLevel() != null) {
				resultDO = getSettingService().getDictionariesById(channel.getLevel());
				DictionariesDO dictionariess = (DictionariesDO) resultDO.getModel(ResultSupport.FIRST_MODEL_KEY);
				channel.setLevelName(dictionariess.getDataItem());
			}
			if (channel.getStatus() != null) {
				resultDO = getSettingService().getDictionariesById(channel.getStatus());
				DictionariesDO dictionaries = (DictionariesDO) resultDO.getModel(ResultSupport.FIRST_MODEL_KEY);
				channel.setStatusName(dictionaries.getDataItem());
			}
			r = channelMapper.updateByPrimaryKeySelective(channel);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "渠道修改失败");
			result.setSuccess(false);
			return result;
		}

		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "渠道修改失败");
			result.setSuccess(false);
			return result;
		}

		result.setModel(ResultDO.FIRST_MODEL_KEY, channel.getId());
		return result;
	}

	@Override
	public ResultDO toSpecialist(ChannelDO channelDO) {
		Channel channel = new Channel();
		ResultSupport result = new ResultSupport();
		if (!result.isSuccess())
			return result;
		int r = -1;
		for (int i = 0; i < channelDO.getIds().length; i++) {
			try {
				channel.setId(channelDO.getIds()[i]);
				channel.setCommissioner(channelDO.getCommissioner());
				if (channelDO.getCommissioner() != null) {
					com.jingyanzi.privilege.domain.result.ResultDO resultEmp = getEmployeeService()
							.getEmployee(channelDO.getCommissioner());

					if (resultEmp.isSuccess()) {
						EmployeeDO employeeDO = (EmployeeDO) resultEmp
								.getModel(com.jingyanzi.privilege.domain.result.ResultSupport.FIRST_MODEL_KEY);
						channel.setCommissionerName(employeeDO.getRealName());
					}
				}
				r = channelMapper.updateByPrimaryKeySelective(channel);
			} catch (Exception e) {
				e.printStackTrace();
				result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "渠道分配专员失败");
				result.setSuccess(false);
				return result;
			}

			if (r < 1) {
				result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "渠道分配专员失败");
				result.setSuccess(false);
				return result;
			}
		}

		result.setModel(ResultDO.FIRST_MODEL_KEY, channel.getId());
		return result;
	}

	@Override
	public ResultDO remove(Integer[] ids) {
		Channel channel = new Channel();

		ResultSupport result = new ResultSupport();
		if (!result.isSuccess())
			return result;
		for (int i = 0; i < ids.length; i++) {
			channel.setId(ids[i]);
			channel.setIsDelete(-1);

			int r = -1;

			try {
				r = channelMapper.updateByPrimaryKeySelective(channel);
			} catch (Exception e) {
				result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "删除渠道失败");
				result.setSuccess(false);
				return result;
			}

			if (r < 1) {
				result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "删除渠道失败");
				result.setSuccess(false);
				return result;
			}
		}

		result.setModel(ResultDO.FIRST_MODEL_KEY, channel.getId());
		return result;
	}

	@Override
	public ResultDO get(Integer id) {
		Channel channel = new Channel();

		ResultSupport result = new ResultSupport();
		if (!result.isSuccess())
			return result;

		try {
			channel = channelMapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取渠道信息失败");
			result.setSuccess(false);
			return result;
		}

		if (channel == null) {
			result.setErrorCode(ResultDO.CHANNEL_GET_ERROR);
			result.setErrorMsg("不存在该渠道");
			result.setSuccess(false);
			return result;
		}
		ChannelDO channelDO = getChannelDO(channel);
		result.setModel(ResultDO.FIRST_MODEL_KEY, channelDO);
		return result;
	}

	@Override
	public ResultDO getChannelByName(String channelName, Integer channelTypeId) {

		ResultSupport result = new ResultSupport();

		ChannelExample example = new ChannelExample();
		Criteria createCriteria = example.createCriteria();
		Criteria createCriteria1 = example.createCriteria();
		if (StringUtils.isNotEmpty(channelName)) {
			createCriteria.andChannelNameEqualTo(channelName);
			createCriteria1.andChannelFullNameEqualTo(channelName);
		}
		if (null != channelTypeId && channelTypeId > 0) {
			createCriteria.andChannelTypeIdEqualTo(channelTypeId);
			createCriteria1.andChannelTypeIdEqualTo(channelTypeId);
		}
		createCriteria.andIsDeleteEqualTo(1);
		createCriteria1.andIsDeleteEqualTo(1);

		example.or(createCriteria1);
		example.setDistinct(true);

		List<Channel> list = null;
		try {
			list = channelMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取渠道信息失败");
			result.setSuccess(false);
			return result;
		}

		if (list == null || list.size() == 0) {
			result.setErrorCode(ResultDO.CHANNEL_GET_ERROR);
			result.setErrorMsg("不存在该渠道");
			result.setSuccess(false);
			return result;
		}
		result.setModel(ResultDO.FIRST_MODEL_KEY, getChannelDOList(list));
		return result;
	}

	public ResultDO query(ChannelQueryDO channelQueryDO) {
		ResultSupport result = new ResultSupport();

		List<Channel> list = null;

		ChannelExample example = new ChannelExample();
		Criteria createCriteria = example.createCriteria();
		Criteria createCriteria1 = example.createCriteria();
		
		if (channelQueryDO.getChannelTypeId() != null) {
			createCriteria.andChannelTypeIdEqualTo(channelQueryDO.getChannelTypeId()); 
			createCriteria1.andChannelTypeIdEqualTo(channelQueryDO.getChannelTypeId()); 
		}
		 
		
		if (channelQueryDO.getLevel() != null) {
			createCriteria.andLevelEqualTo(channelQueryDO.getLevel());
			createCriteria1.andLevelEqualTo(channelQueryDO.getLevel());
		}

		if (StringUtils.isNotEmpty(channelQueryDO.getChannelName())) {
			createCriteria.andChannelNameLike("%" + channelQueryDO.getChannelName() + "%");
			createCriteria1.andChannelFullNameLike("%" + channelQueryDO.getChannelName() + "%");
		}

		if (channelQueryDO.getChannelTypeName() != null) {
			createCriteria.andChannelTypeNameEqualTo(channelQueryDO.getChannelTypeName());
			createCriteria1.andChannelTypeNameEqualTo(channelQueryDO.getChannelTypeName());
		}
		if (channelQueryDO.getCommissioner() != null) {
			createCriteria.andCommissionerEqualTo(channelQueryDO.getCommissioner());
			createCriteria1.andCommissionerEqualTo(channelQueryDO.getCommissioner());
		}
		if (channelQueryDO.getProvince() != null) {
			if (StringUtils.isNotEmpty(channelQueryDO.getProvince())) {
				createCriteria.andProvinceEqualTo(channelQueryDO.getProvince());
				createCriteria1.andProvinceEqualTo(channelQueryDO.getProvince());
			}
		}
		if (channelQueryDO.getCity() != null) {
			if (StringUtils.isNotEmpty(channelQueryDO.getCity())) {
				createCriteria.andCityEqualTo(channelQueryDO.getCity());
				createCriteria1.andCityEqualTo(channelQueryDO.getCity());
			}
		}
		if (channelQueryDO.getDistrict() != null) {
			if (StringUtils.isNotEmpty(channelQueryDO.getDistrict())) {
				createCriteria.andDistrictEqualTo(channelQueryDO.getDistrict());
				createCriteria1.andDistrictEqualTo(channelQueryDO.getDistrict());
			}
		}
		createCriteria.andIsDeleteEqualTo(1);
		createCriteria1.andIsDeleteEqualTo(1);
		
		example.or(createCriteria1);
		example.setDistinct(true);
		List<ChannelDO> channelDOs = null;

		if (channelQueryDO.isPage()) {
			example.setLimitByPage(channelQueryDO.getStart() + "," + channelQueryDO.getPageRows());
			long count = channelMapper.countByExample(example);
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
		}

		try {
			list = channelMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取渠道信息失败");
			result.setSuccess(false);
			return result;
		}
		channelDOs = getChannelDOList(list);
		result.setModel(ResultSupport.FIRST_MODEL_KEY, channelDOs);

		return result;
	}

	// 简称去重
	public ResultDO checkRepeat(ChannelDO domain) {

		ResultSupport result = new ResultSupport();
		ChannelExample example = new ChannelExample();
		Criteria criteria = example.createCriteria();

		if (isNumberNotEmpty(domain.getId())) {
			criteria.andIdNotEqualTo(domain.getId());
		}
		if (StringUtils.isNotEmpty(domain.getChannelName())) {
			criteria.andChannelNameEqualTo(domain.getChannelName());
		}
		if (isNumberNotEmpty(domain.getChannelTypeId())) {
			criteria.andChannelTypeIdEqualTo(domain.getChannelTypeId());
		}
		if (StringUtils.isNotEmpty(domain.getProvince())) {
			criteria.andProvinceEqualTo(domain.getProvince());
		}
		if (StringUtils.isNotEmpty(domain.getCity())) {
			criteria.andCityEqualTo(domain.getCity());
		}
		if (StringUtils.isNotEmpty(domain.getDistrict())) {
			criteria.andDistrictEqualTo(domain.getDistrict());
		}
		criteria.andIsDeleteEqualTo(1);

		int count = 0;
		try {
			count = channelMapper.countByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "分类查询失败");
			result.setSuccess(false);
			return result;
		}

		if (count > 0) {
			result.setModel(ResultDO.FIRST_MODEL_KEY, count);
			return result;
		}
		result.setModel(ResultDO.FIRST_MODEL_KEY, 0);

		return result;
	}

	// 全称去重
	public ResultDO checkFullNameRepeat(ChannelDO domain) {

		ResultSupport result = new ResultSupport();
		ChannelExample example = new ChannelExample();
		Criteria criteria = example.createCriteria();

		if (isNumberNotEmpty(domain.getId())) {
			criteria.andIdNotEqualTo(domain.getId());
		}
		if (StringUtils.isNotEmpty(domain.getChannelFullName())) {
			criteria.andChannelFullNameEqualTo(domain.getChannelFullName());
		}
		criteria.andIsDeleteEqualTo(1);

		int count = 0;
		try {
			count = channelMapper.countByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "分类查询失败");
			result.setSuccess(false);
			return result;
		}

		if (count > 0) {
			result.setModel(ResultDO.FIRST_MODEL_KEY, count);
			return result;
		}
		result.setModel(ResultDO.FIRST_MODEL_KEY, 0);

		return result;
	}
}
